<template>
  <a-modal
    title="添加账号"
    :width="600"
    :visible="visible"
    :destroyOnClose="true"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-alert
      class="errorAlert"
      v-if="alertVisable"
      showIcon
      banner
      message="保存账号信息失败,请账号信息是否填写正确"
      type="error"
      closable
      @close="alertOnClose"
    />
    <a-form :form="form">
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="账号所属应用"
          >
            <a-select
              showSearch
              placeholder="选择应用"
              optionFilterProp="children"
              style="width: 200px"
              :filterOption="filterOption"
              v-decorator="['appId', {}]"
            >
              <a-select-option v-for="item in huiwAppIds" :value="item.appId" :key="item.appId">
                {{ item.appInfo }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="账号名"
          >
            <a-input v-decorator="['accountId', {rules: [{ required: true, message: '请填写账号名' }]}]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="密码"
          >
            <a-input v-decorator="['accountPasswd', {rules: [{ required: true, message: '请填写密码' }]}]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="账号昵称"
          >
            <a-input v-decorator="['accountName', {rules: [{ required: true, message: '请填写一个昵称' }]}]" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script>
import { saveAccountInfo } from '../account'

export default {
  name: 'AccountNewModal',
  props: {
  },
  data () {
    return {
      // form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      personPid: '0',
      confirmLoading: false,
      alertVisable: false,
      visible: false,
      huiwAppIds: [
        { appId: 'huiwcore', appInfo: '平台应用' }
      ],
      // accountTypes: ['passwd'],
      accountColumns: [
        {
          title: '应用名称',
          dataIndex: 'appId',
          customRender: function (text) {
            if (text === 'huiwcore') {
              return '平台应用'
            } else {
              return text
            }
          }
        },
        {
          title: '昵称',
          dataIndex: 'accountName'
        },
        {
          title: '账号名',
          dataIndex: 'accountId'
        },
        {
          title: '账号类型',
          dataIndex: 'accountType',
          customRender: function (text) {
            if (text === 'passwd') {
              return '账号密码'
            } else if (text === 'wx') {
              return '微信授权'
            } else if (text === 'wxXcx') {
              return '微信小程序'
            } else if (text === 'wxWork') {
              return '企业微信'
            } else if (text === 'aliPay') {
              return '支付宝授权'
            } else if (text === 'aliXcx') {
              return '支付宝小程序'
            } else {
              return text
            }
          }
        },
        {
          title: '到期时间',
          dataIndex: 'expireTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      accountsData: []
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {
  },
  methods: {
    showModal (personPid) {
      console.log(personPid)
      this.personPid = personPid
      this.visible = true
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleOk () {
      const _this = this
      // 触发表单验证
      _this.form.validateFields((err, values) => {
        // 验证表单没错误
        if (!err) {
          console.log('form values', values)
          _this.confirmLoading = true
          new Promise((resolve) => {
            resolve(saveAccountInfo(Object.assign({ personPid: this.personPid }, values), {}))
          }).then((res) => {
            if (res.code === 'C1') {
              _this.$message.success('保存成功')
              _this.handleCancel()
              _this.$emit('ok')
            } else {
              _this.alertVisable = true
            }
            // Do something
            _this.confirmLoading = false
            // _this.$emit('ok')
          }).catch(() => {
            // Do something
          }).finally(() => {
            // Do something
          })
        }
      })
    },
    handleCancel () {
      this.$emit('close')
      this.visible = false
    },
    // 关闭提示
    alertOnClose () {
      this.alertVisable = false
    }
  }
}
</script>

<style>
  .avatar-uploader > .ant-upload {
    width: 200px;
    height: 200px;
  }
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }
  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
  .errorAlert {
    margin-top: 8px;
  }
</style>
